package service;

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

import model.FiyatiNeydi;
import model.User;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
import com.mongodb.MongoURI;

public class DAO {

	private final static Mongo getMongoDB() throws UnknownHostException,
			MongoException {
		try {
			// Mongo m = new Mongo();
			MongoURI uri = new MongoURI(
					"mongodb://fiyatineydi:Fiyatineydi@ds035127.mongolab.com:35127/fiyatineydi");
			System.out.println(uri);
			Mongo m = new Mongo(uri);
			System.out.println("return m");
			return m;
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return null;
		}
	}

	public final static String testDB() throws UnknownHostException,
			MongoException {
		String s = "";
		Mongo m = new Mongo();
		// DB db = DAO.getMongoDB().getDB("fiyatineydi");
		DB db = m.getDB("fiyatineydi");
		DBCollection coll = db.getCollection("fiyatlar");
		s += "fiyat sayısı: " + coll.getCount();
		return s;

	}

	public final static List<FiyatiNeydi> getFiyatlar()
			throws UnknownHostException, MongoException {
		System.out.println("getFiyatlar çağırıldı");
		List<FiyatiNeydi> fiyatiNeydis = new ArrayList<FiyatiNeydi>();
	//	fiyatiNeydis.add(new FiyatiNeydi("q", "w", "u", 7));
		Mongo m = new Mongo();
		// DB db = DAO.getMongoDB().getDB("fiyatineydi");
		DB db = m.getDB("fiyatineydi");
		System.out.println("getDB");
		// String username="fiyatineydi";
		// char[] passwd=new char[11];
		// passwd="Fiyatineydi".toCharArray();
		// db.authenticate(username, passwd);

		System.out.println("isAuthenticated: " + db.isAuthenticated());
		// if (db.isAuthenticated()) {
		DBCollection coll = db.getCollection("fiyatlar");
		System.out.println("getCount : " + coll.getCount());
		DBCursor cur = coll.find();

		while (cur.hasNext()) {
			DBObject a = cur.next();
			if (a.toString().indexOf("market") > -1) {
				System.out.println(a.toString());
				String market = a.get("market").toString();
				System.out.println(a.get("market").toString());
				String user = a.get("user").toString();
				String urun = a.get("urun").toString();
				String _id = a.get("_id").toString();
				double fiyat = (Double) a.get("fiyat");
				fiyatiNeydis.add(new FiyatiNeydi(_id,urun, market, user, fiyat));
			}
			// System.out.println(cur.next().get("urun"));
		}
		System.out.println("size : " + fiyatiNeydis.size());
		// } else {
		// System.out.println("authenticate olamadım");
		// }
		System.out.println("end");
		return fiyatiNeydis;

	}

	public final static List<User> getUsers() throws UnknownHostException,
			MongoException {
		List<User> users = new ArrayList<User>();
		Mongo m = new Mongo();
		DB db = m.getDB("fiyatineydi");
		DBCollection coll = db.getCollection("users");
		DBCursor cur = coll.find();

/*
		while (cur.hasNext()) {
			DBObject a = cur.next();
			coll.remove(a);
		}
*/
		System.out.println(cur.count());
		while (cur.hasNext()) {
			DBObject a = cur.next();
			System.out.println(a.toString());
			if (a.get("uid") != null) {
				User user = new User();
				String access_token = a.get("access_token").toString();
				user.setAccess_token(access_token);
				String name = a.get("name").toString();
				user.setName(name);
				String uid = a.get("uid").toString();
				user.setUid(uid);
				user.setPic_square(a.get("pic_square").toString());
				
				users.add(user);
			}
		}
		return users;

	}

	public final static void insertFiyat(FiyatiNeydi fiyat)
			throws UnknownHostException, MongoException {
		System.out.println("insertFiyat çağırıldı");
		Mongo m = new Mongo();

		// DB db = DAO.getMongoDB().getDB("fiyatineydi");
		DB db = m.getDB("fiyatineydi");
		System.out.println("getDB");
		// String username="fiyatineydi";
		// char[] passwd=new char[11];
		// passwd="Fiyatineydi".toCharArray();
		// db.authenticate(username, passwd);

		// System.out.println("isAuthenticated: "+db.isAuthenticated());
		// if (db.isAuthenticated()) {

		DBCollection coll = db.getCollection("fiyatlar");
		BasicDBObject dbo = new BasicDBObject();
		BasicDBObject dbObject = new BasicDBObject();
		dbObject.put("fiyat", fiyat.getFiyat());
		dbObject.put("market", fiyat.getMarket());
		dbObject.put("urun", fiyat.getUrun());
		dbObject.put("user", fiyat.getUser());
		dbo.put("fiyat", dbObject);
		// coll.insert(dbo);
		coll.insert(dbObject);
		// } else {System.out.println("authenticate olamadım - insert");}
	}

	public final static void insertUser(User user) throws UnknownHostException,
			MongoException {
		System.out.println("insertUser çağırıldı");
		Mongo m = new Mongo();

		// DB db = DAO.getMongoDB().getDB("fiyatineydi");
		DB db = m.getDB("fiyatineydi");

		// String username="fiyatineydi";
		// char[] passwd=new char[11];
		// passwd="Fiyatineydi".toCharArray();
		// db.authenticate(username, passwd);

		// System.out.println("isAuthenticated: "+db.isAuthenticated());
		// if (db.isAuthenticated()) {
		DBCollection coll = db.getCollection("users");
		// BasicDBObject dbo = new BasicDBObject();
		BasicDBObject dbObject = new BasicDBObject();
		dbObject.put("uid", user.getUid());
		dbObject.put("name", user.getName());
		dbObject.put("access_token", user.getAccess_token());
		dbObject.put("pic_square", user.getPic_square());
		// dbo.put("user",dbObject);
		System.out.println("#"+dbObject.toString());
		System.out.println("##"+DAO.getUserByUid(user.getUid()));
		if(DAO.getUserByUid(user.getUid()).getUid()!=user.getUid())
		{
			System.out.println("bulamadım");
			coll.insert(dbObject);
		}
		else
		{
			System.out.println("buldum");
		}
		// } else {System.out.println("authenticate olamadım - insert");}
	}

	public List<FiyatiNeydi> searchFiyatlar(String search) {
		// TODO
		return null;
	}

	public final static User getUserByUid(String uid) throws UnknownHostException, MongoException {
		Mongo m = new Mongo();
		DB db = m.getDB("fiyatineydi");
		DBCollection coll = db.getCollection("users");
		BasicDBObject o = new BasicDBObject();
		o.put("uid", uid);
		DBCursor cursor = coll.find(o);

     	User user = new User();
    	
            while(cursor.hasNext()) {
            	DBObject a = cursor.next();
   			String access_token = a.get("access_token").toString();
				user.setAccess_token(access_token);
				String name = a.get("name").toString();
				user.setName(name);
				user.setUid(uid);
				user.setPic_square(a.get("pic_square").toString());
            }
				return user;
           
        
	}

	public static void deleteUser(String parameter) throws UnknownHostException, MongoException {
		System.out.println("delete user" + parameter);
		Mongo m = new Mongo();
		DB db = m.getDB("fiyatineydi");
		DBCollection coll = db.getCollection("users");
		BasicDBObject o = new BasicDBObject();
		o.put("uid", parameter);
		coll.remove(o);		
	}

	public static void deleteFiyat(String parameter) throws UnknownHostException, MongoException {
		// TODO Auto-generated method stub
		System.out.println("delete fiyat" + parameter);
		Mongo m = new Mongo();
		DB db = m.getDB("fiyatineydi");
		DBCollection coll = db.getCollection("fiyatlar");
		BasicDBObject o = new BasicDBObject();
		o.put("market", parameter);
		coll.remove(o);		
	}
}
